package com.cloud.school.app.config;

import com.cloud.school.app.util.IdWorker;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;


@Configuration
public class IdWorkerConfiguration {

    @Bean(name = "answerIdWorker")
    public IdWorker answerIdWorkerFactory() {
        return new IdWorker(getWorkId(), 1);
    }

    @Bean(name = "collegeIdWorker")
    public IdWorker collegeIdWorkerFactory() {
        return new IdWorker(getWorkId(), 2);
    }

    @Bean(name = "commentIdWorker")
    public IdWorker commentIdWorkerFactory() {
        return new IdWorker(getWorkId(), 3);
    }

    @Bean(name = "friendshipIdWorker")
    public IdWorker friendshipIdWorkerFactory() {
        return new IdWorker(getWorkId(), 4);
    }

    @Bean(name = "groupIdWorker")
    public IdWorker groupIdWorkerFactory() {
        return new IdWorker(getWorkId(), 5);
    }

    @Bean(name = "groupMemberIdWorker")
    public IdWorker groupMemberIdWorkerFactory() {
        return new IdWorker(getWorkId(), 6);
    }

    @Bean(name = "imageIdWorker")
    public IdWorker imageIdWorkerFactory() {
        return new IdWorker(getWorkId(), 7);
    }

    @Bean(name = "likeIdWorker")
    public IdWorker likeIdWorkerFactory() {
        return new IdWorker(getWorkId(), 8);
    }

    @Bean(name = "majorIdWorker")
    public IdWorker majorIdWorkerFactory() {
        return new IdWorker(getWorkId(), 9);
    }

    @Bean(name = "messageIdWorker")
    public IdWorker messageIdWorkerFactory() {
        return new IdWorker(getWorkId(), 10);
    }

    @Bean(name = "questionIdWorker")
    public IdWorker questionIdWorkerFactory() {
        return new IdWorker(getWorkId(), 11);
    }

    @Bean(name = "showIdWorker")
    public IdWorker showIdWorkerFactory() {
        return new IdWorker(getWorkId(), 12);
    }

    @Bean(name = "userIdWorker")
    public IdWorker userIdWorkerFactory() {
        return new IdWorker(getWorkId(), 13);
    }

    @Bean(name = "chatRoomIdWorker")
    public IdWorker chatRoomIdWorkerFactory() {
        return new IdWorker(getWorkId(), 13);
    }


    private static long getWorkId() {
        try {
            InetAddress addr = InetAddress.getLocalHost();
            String ip = addr.getHostAddress().toString(); //获取本机ip
            String[] strings = ip.split("\\.");
            String num = strings[strings.length - 1];
            if (Integer.valueOf(num) <= 31) {
                return Long.valueOf(num);

            } else {
                return Long.valueOf(num) % 31L;
            }
        } catch (Exception e) {
            return 1L;
        }
    }
}
